import json

# 2.从需求关键词视角，分析在不同标注 Tag 或者 Category 类别中， 编程开发人员的组合需求（Mashup）与该需求所调用的服务（Web API）的关联情况
print("从需求关键词视角，分析在不同标注 Tag 或者 Category 类别中， 编程开发人员的组合需求（Mashup）与该需求所调用的服务（Web API）的关联情况")
print("数据来自 active_mashups_data.txt、deadpool_mashups_data.txt")

print("统计 Mashup 对应 category 以及其包含的 API")
mashupTitleList = []
categoryList = []
apiTitleList = []
categoryCount = {}

txtFile1 = open(r"../data/raw/api_mashup/active_mashups_data.txt")
txtFile2 = open(r"../data/raw/api_mashup/deadpool_mashups_data.txt")
txtContentList = [txtFile1.read(), txtFile2.read()]

for txtContent in txtContentList:
    jsondata = json.loads(txtContent)

    for mashup in jsondata:
        if mashup != None:
            mashupTitleList.append(mashup["title"][8:])
            categories = mashup["categories"]
            categoryList.append(categories)
            for category in categories:
                if category != None:
                    if category in categoryCount:
                        categoryCount[category] += 1
                    else:
                        categoryCount[category] = 1

            related_apis = mashup["related_apis"]
            tempApiTitleList = []
            for api in related_apis:
                if api != None:
                    tempApiTitleList.append(api["title"])
            apiTitleList.append(tempApiTitleList)

print("--------------------------------Mashup 对应 category 以及其包含的 API--------------------------------")
mashupCategoryApiCount = zip(mashupTitleList, categoryList, apiTitleList)
keywordsRelatedApiUsedCount = {"Mapping":{}, "Search":{}}
for item in mashupCategoryApiCount:
    print(item)
    for key,value in keywordsRelatedApiUsedCount.items():
        if key in item[1]:
            for api in item[2]:
                if api in value:
                    value[api] += 1
                else:
                    value[api] = 1

print("-----------------------------------分析编程开发人员的组合需求（Mashup）-----------------------------------")
categoryCountList = sorted(categoryCount.items(), key=lambda x:x[1], reverse=True)
for item in categoryCountList:
    print(item)

print("-----------------------------------分析需求关键字如（Mapping、Search）所调用的服务（Web API）-----------------------------------")
for key,value in keywordsRelatedApiUsedCount.items():
    print("------------------------------------以 " + key + " 为例分析相关 API -------------------------------------")
    apiList = sorted(value.items(), key = lambda x:x[1], reverse = True)
    for item in apiList:
        print(item)